package com.anycl.spring.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.HashMap;
import java.util.Map;

@Aspect
public class TrackCounter {

    private Map<Integer, Integer> trackCounts =
            new HashMap<>();

    /**
     * 处理通知中的参数
     * @param trackNumber
     */
    @Pointcut("execution(* com.anycl.spring.aop.Blue.playTrack(int))" +
            "&& args(trackNumber)")
    public void trackPlayed(int trackNumber) {

    }

    @Before("trackPlayed(trackNumber)")
    public void countTrack(int trackNumber) {
        int currentCount = getPlayCount(trackNumber);
        trackCounts.put(trackNumber, currentCount);
    }

    public int getPlayCount(int trackNumber) {
        return trackCounts.containsKey(trackNumber)? trackCounts.get(trackNumber) + 1 : 0;
    }
}
